package com.symaster.common.connect.test;

import com.symaster.common.connect.tcp.TcpElementW;
import com.symaster.common.swing.util.SwingUtil;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

/**
 * @author yinmiao
 * @version 2022-05-03 15:57
 */
public class TcpTestW {
    private JPanel panel1;
    private JTextField textField1;
    private JButton button1;
    private JLabel text;
    private final TcpElementW tcpElementW;

    public TcpTestW() {
        try {
            tcpElementW = new TcpElementW("127.0.0.1", TestCommon.masterPort);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        button1.addActionListener(e -> {
            try {
                text.setText(tcpElementW.sendAndWait(textField1.getText()));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("TcpTestW");
        frame.setContentPane(new TcpTestW().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        SwingUtil.toCenter(frame);
        frame.setVisible(true);
    }

    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        panel1 = new JPanel();
        panel1.setLayout(new BorderLayout(0, 0));
        text = new JLabel();
        text.setText("Label");
        panel1.add(text, BorderLayout.SOUTH);
        final JToolBar toolBar1 = new JToolBar();
        panel1.add(toolBar1, BorderLayout.CENTER);
        textField1 = new JTextField();
        toolBar1.add(textField1);
        button1 = new JButton();
        button1.setText("Button");
        toolBar1.add(button1);
    }

    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return panel1;
    }

}
